package com.yt.sys.server.api.cms;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yt.common.constant.Result;
import com.yt.sys.models.dto.SysAdminRoleDTO;
import com.yt.sys.models.entity.SysAdminRole;
import com.yt.sys.models.service.ISysAdminRoleService;
import com.yt.sys.models.service.ISysAdminService;
import com.yt.sys.models.service.ISysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/cms/sys/admin-role")
@Api(tags = "系统管理-管理员角色关联管理")
public class SysAdminRoleController {

    @Autowired
    private ISysAdminRoleService sysAdminRoleService;

    @Autowired
    private ISysAdminService sysAdminService;

    @Autowired
    private ISysRoleService sysRoleService;

    @GetMapping("/list")
    @ApiOperation("管理员角色关联列表")
    public Result<List<SysAdminRole>> list() {
        QueryWrapper<SysAdminRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        List<SysAdminRole> list = sysAdminRoleService.list(queryWrapper);
        return Result.success(list);
    }

    @GetMapping("/admin/{adminId}")
    @ApiOperation("根据管理员ID获取角色列表")
    public Result<List<SysAdminRole>> getByAdminId(@PathVariable Integer adminId) {
        // 检查管理员是否存在
        if (sysAdminService.getById(adminId) == null) {
            return Result.fail("管理员不存在");
        }

        QueryWrapper<SysAdminRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("admin_id", adminId);
        queryWrapper.eq("is_delete", 0);
        List<SysAdminRole> list = sysAdminRoleService.list(queryWrapper);
        return Result.success(list);
    }

    @PostMapping
    @ApiOperation("新增管理员角色关联")
    public Result<String> add(@Valid @RequestBody SysAdminRoleDTO sysAdminRoleDTO) {
        // 检查管理员是否存在
        if (sysAdminService.getById(sysAdminRoleDTO.getAdminId()) == null) {
            return Result.fail("管理员不存在");
        }

        // 检查角色是否存在
        if (sysRoleService.getById(sysAdminRoleDTO.getRoleId()) == null) {
            return Result.fail("角色不存在");
        }

        // 检查是否已存在关联关系
        QueryWrapper<SysAdminRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("admin_id", sysAdminRoleDTO.getAdminId());
        queryWrapper.eq("role_id", sysAdminRoleDTO.getRoleId());
        queryWrapper.eq("is_delete", 0);
        if (sysAdminRoleService.count(queryWrapper) > 0) {
            return Result.fail("管理员角色关联关系已存在");
        }

        SysAdminRole adminRole = new SysAdminRole();
        BeanUtils.copyProperties(sysAdminRoleDTO, adminRole);
        adminRole.setIsDelete(0);
        boolean result = sysAdminRoleService.save(adminRole);
        if (result) {
            return Result.success("关联成功");
        } else {
            return Result.fail("关联失败");
        }
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除管理员角色关联")
    public Result<String> delete(@PathVariable Integer id) {
        SysAdminRole adminRole = sysAdminRoleService.getById(id);
        if (adminRole == null || adminRole.getIsDelete() == 1) {
            return Result.fail("管理员角色关联关系不存在");
        }

        // 逻辑删除
        adminRole.setIsDelete(1);
        boolean result = sysAdminRoleService.updateById(adminRole);
        if (result) {
            return Result.success("删除成功");
        } else {
            return Result.fail("删除失败");
        }
    }

    @DeleteMapping("/admin/{adminId}/role/{roleId}")
    @ApiOperation("根据管理员ID和角色ID删除关联关系")
    public Result<String> deleteByAdminIdAndRoleId(@PathVariable Integer adminId, @PathVariable Integer roleId) {
        QueryWrapper<SysAdminRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("admin_id", adminId);
        queryWrapper.eq("role_id", roleId);
        queryWrapper.eq("is_delete", 0);
        
        SysAdminRole adminRole = sysAdminRoleService.getOne(queryWrapper);
        if (adminRole == null) {
            return Result.fail("删除失败，关联关系不存在");
        }

        // 逻辑删除
        adminRole.setIsDelete(1);
        boolean result = sysAdminRoleService.updateById(adminRole);
        if (result) {
            return Result.success("删除成功");
        } else {
            return Result.fail("删除失败");
        }
    }
}